(function(window, undefined){

var egame = {};

egame.add = function (name) {
    //拆分名字空间域字符串
    var domains = name.split(".");
    var cur_domain = egame;
    //循环遍历每一级子域
    for(var i=0; i< domains.length; i++) {
        var domain = domains[i];
        //如果该域的空间未被创建
        if(typeof(cur_domain[domain]) == "undefined") {
            //创建域
            cur_domain[domain] = {};
        }
        //设置当前域为此次循环的域
        cur_domain = cur_domain[domain];
    }
    return cur_domain;
};

//前端模版
egame.tmpl = (function(){
  var cache = {};

  var tmpl = function tmpl(str, data){
    // Figure out if we're getting a template, or if we need to
    // load the template - and be sure to cache the result.
    var fn = !/\W/.test(str) ?
      cache[str] = cache[str] ||
        tmpl(document.getElementById(str).innerHTML) :

      // Generate a reusable function that will serve as a template
      // generator (and which will be cached).
      new Function("obj",
        "var p=[],print=function(){p.push.apply(p,arguments);};" +

        // Introduce the data as local variables using with(){}
        "with(obj){p.push('" +

        // Convert the template into pure JavaScript
        str
          .replace(/[\r\t\n]/g, " ")
          .split("<%").join("\t")
          .replace(/((^|%>)[^\t]*)'/g, "$1\r")
          .replace(/\t=(.*?)%>/g, "',$1,'")
          .split("\t").join("');")
          .split("%>").join("p.push('")
          .split("\r").join("\\'")
      + "');}return p.join('');");

    // Provide some basic currying to the user
    return data ? fn( data ) : fn;
  };
  return tmpl;
})();

/*
* HomePageFavorite-0.0.0.1
*/
egame.homepageFavorite = {
    //设为首页
    Homepage: function () {
        if (document.all) {
            document.body.style.behavior = 'url(#default#homepage)';
            document.body.setHomePage(window.location.href);
        }else if (window.sidebar) {
            if (window.netscape) {
                try {
                    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
                }catch (e) {
                    alert("该操作被浏览器拒绝，如果想启用该功能，请在地址栏内输入 about:config,然后将项 signed.applets.codebase_principal_support 值该为true");
                }
            }
            var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
            prefs.setCharPref('browser.startup.homepage', window.location.href);
        }
    }
    ,
    //加入收藏
    Favorite: function Favorite(sURL, sTitle) {
        try {
            window.external.addFavorite(sURL, sTitle);
        }
        catch (e) {
            try {
                window.sidebar.addPanel(sTitle, sURL, "");
            }
            catch (e) {
               alert("加入收藏失败,请手动添加.");
            }
        }
    }
};

window.Egame = egame;


})(window);
